﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CLEXA.DataContracts;
using CLEXA.SurfaceClient.Functions;

namespace CLEXA.SurfaceClient.ViewModel
{
    public class SchemaVM : VMBase
    {
        private readonly ISchema facade;
        private readonly ReadOnlyCollection<TableVM> tables;
        private readonly RelayCommand back;

        public IEnumerable<TableVM> Tables
        {
            get { return tables; }
        }

        public ICommand Back
        {
            get { return back; }
        }

        public SchemaVM(ISchema facade)
        {
            this.facade = facade;
            this.tables = new ReadOnlyCollection<TableVM>(facade.Tables.OrderBy(p => p.Catalog).ThenBy(p => p.Schema).ThenBy(p => p.Name).Select(p => new TableVM(p.Catalog, p.Schema, p.Name, p.Columns.Select(x => new ColumnVM(x.Name, x.DataType)))).ToList());
            this.back = new RelayCommand(DoBack);
        }

        private void DoBack()
        {
            facade.GoToHome();
        }
    }
}
